﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MixScript.BLL;

namespace MixScript.BLL.FI
{
    public class GroupCOA : cBaseBLL
    {

        public GroupCOA()
        { }
        /// <summary>
        /// cGroup
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public bool getGroupCOA(ref DataTable dt)
        {
            try
            {

                dt = new DataTable();
                string sSQL = @"SELECT * FROM
                            (SELECT null Parentid, g.groupnum as idcoa, g.groupname coaName FROM accgroup g
                            union
                            SELECT s.groupnum Parentid,s.subnum idcoa,s.subname coaName FROM accsubgroup s) t
                            ORDER BY Parentid,idcoa, coaName";
                
                dt = Dbserver.ExecuteQuery(sSQL, "vw_coamst");
                

                return dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        /// <summary>
        /// cGroup
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="Groupid"></param>
        /// <returns></returns>
        public bool getGroupCOA(ref DataTable dt, int Groupid)
        {
            try
            {
                dt = new DataTable();
                string sSQL = @"SELECT * FROM accgroup g
                            where groupnum = {0}";
                dt = Dbserver.ExecuteQuery(string.Format(sSQL, Groupid), "vw_coamst");
                return dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// cSubGroup
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="SubGroupid"></param>
        /// <returns></returns>
        public bool getSubGroupCOA(ref DataTable dt, int SubGroupid)
        {
            try
            {
                dt = new DataTable();
                string sSQL = @"SELECT * FROM accsubgroup s
                            where s.subnum  = {0}";
                dt = Dbserver.ExecuteQuery(string.Format(sSQL, SubGroupid), "vw_coamst");
                return dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// cSubGroup
        /// </summary>
        /// <param name="GroupID"></param>
        /// <param name="name"></param>
        /// <param name="acc"></param>
        /// <param name="isNew"></param>
        /// <returns></returns>
        public bool saveGroup(Int32 GroupID, string name, string acc, bool isNew)
        {
            string sql;
            if (isNew)
                sql = "INSERT INTO accgroup (groupnum,groupname,aplication) VALUES ('{0}','{1}','{2}')";
            else
                sql = "UPDATE accgroup SET groupname='{1}',aplication='{2}' WHERE groupnum = {0}";
            try
            {
                Dbserver.ExecuteScalar(String.Format(sql, GroupID, name, acc));
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }
        public bool saveSubGroup(Int32 groupID, Int32 subGroupID, string name, string acc, string subAcc, bool isNew)
        {

            string sql;
            if (isNew)
                sql = "INSERT INTO accsubgroup (subnum,groupnum,subname,aplication,subaplication) VALUES ('{0}','{1}','{2}','{3}','{4}')";
            else
                sql = "UPDATE accsubgroup SET groupnum={1},subname='{2}',aplication='{3}',subaplication='{3}' WHERE subnum = {0}";
            try
            {
                Dbserver.ExecuteScalar(String.Format(sql, subGroupID, groupID, name, acc, subAcc));
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }





    }
}
